local ActDefine = require("activity.Define")
local Util = require("common.Util")
local Config = require("Config")
local Banshu = require("common.Banshu")

RunningActList = RunningActList or {_today = {}}
CallBackList = CallBackList or {}

function onProcessActState(nData)
    RunningActList._today[nData.actConfigID] = nData 
    onCallBack(nData.actConfigID, nData.status, nData.actArgs)
end

function onCallBack(actConfigID, status, actArgs)
    local nCallBackData = CallBackList[actConfigID]
    if nCallBackData and nCallBackData[status] then
        nCallBackData[status](actArgs, nCallBackData.args)
    end
end

function onCheckActStatus(actConfigID)
    return RunningActList._today[actConfigID] and RunningActList._today[actConfigID].status or ActDefine.ACT_STATE_READY 
end

function onCheckActCanJoin(human, actConfigID)
    if onCheckActStatus(actConfigID) ~= ActDefine.ACT_STATE_RUNNING then
        return false
    end
    return true 
end

function onCheckActIsRunning(actConfigID)
    if onCheckActStatus(actConfigID) ~= ActDefine.ACT_STATE_RUNNING then
        return false
    end
    return true
end

function onGetActData(actConfigID)
    if not RunningActList._today[actConfigID] then
        return nil
    end

    return RunningActList._today[actConfigID]
end

function onGetCurActRuleID(actConfigID)
    if onCheckActIsRunning(actConfigID) then
        local nActData = onGetActData(actConfigID)
        if nActData then
            return nActData.actArgs.ruleid
        end
    end
    return -1
end

function onGetCurActVersion(actConfigID)
    local nActData = onGetActData(actConfigID)
    if nActData then
        return nActData.actArgs.version or 0
    end
end

function canShow()
    if Banshu.IS_BANSHU then
      return
    end
    if Config.IS_MIDDLE then
        return
    end
    for k, v in pairs(RunningActList._today) do
        if v.status == ActDefine.ACT_STATE_RUNNING then
            return true 
        end
    end
    return false
end

function onRegisterCallBack(actConfigID, onBeginBefore, onRunning, onEnd, actArgs)
    CallBackList[actConfigID] = {[ActDefine.ACT_STATE_BEFORESTART] = onBeginBefore, [ActDefine.ACT_STATE_RUNNING] = onRunning, [ActDefine.ACT_STATE_ENDED] = onEnd, args = actArgs}
end

